package com.javabasic.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/11/14 7:36 下午
 * @see [677. Map Sum Pairs](https://leetcode-cn.com/problems/map-sum-pairs/)
 */
public class MapSumPairs {
}

/**
 * 暴力法+HashMap
 */
class MapSum {

    Map<String, Integer> keyVal;

    public MapSum() {
        keyVal = new HashMap<>(16);
    }

    public void insert(String key, int val) {
        keyVal.put(key,val);
    }

    public int sum(String prefix) {
        AtomicInteger result = new AtomicInteger();
        if (!keyVal.isEmpty()) {
            keyVal.forEach((key, val) -> {
                if (val != null && key.startsWith(prefix)) {
                    result.addAndGet(val);
                }
            });
        }
        return result.get();
    }
}

/**
 * Your MapSum object will be instantiated and called as such:
 * MapSum obj = new MapSum();
 * obj.insert(key,val);
 * int param_2 = obj.sum(prefix);
 */